package practise.mulitThreads.ConcurrentInPractice.secondTime;

import think.in.java.concurrency.practise.exec6.LockExec;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by Richard on 7/28/2014.
 */
public class ReadWriteMap<K,V> {
    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    Lock readLock = readWriteLock.readLock();
    Lock writeLock = readWriteLock.writeLock();
    Map<K,V> map =  new HashMap<>();
    public V get(K key){
        try{
            readLock.lock();
             return map.get(key);
        }
        finally {
            readLock.unlock();
        }
    }

    public void put(K key, V value){
        try{
            writeLock.lock();
            map.put(key,value);
        }
        finally {
            writeLock.unlock();
        }
    }
}
